package com.luke.exercise.play;

public class ThirdBig {
    public int thirdMax(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return nums[0] > nums[1] ? nums[0] : nums[1];
        }
        int max = nums[0];
        int second = nums[1];
        int third = nums[2];
        if (nums[0] >= nums[2] && nums[2] >= nums[1]) {
            second = nums[2];
            third = nums[1];
        }
        if (nums[1] >= nums[0] && nums[0] >= nums[2]) {
            max = nums[1];
            second = nums[0];
            third = nums[2];
        }
        if (nums[1] >= nums[2] && nums[2] >= nums[0]) {
            max = nums[1];
            second = nums[2];
            third = nums[0];
        }
        if (nums[2] >= nums[1] && nums[1] >= nums[0]) {
            max = nums[2];
            second = nums[1];
            third = nums[0];
        }
        if (nums[2] >= nums[0] && nums[0] >= nums[1]) {
            max = nums[2];
            second = nums[0];
            third = nums[1];
        }
        if (nums.length > 3) {
            for (int i = 3; i < nums.length; i++) {
                if (nums[i] > third && nums[i] < second) {
                    third = nums[i];
                }
                if (nums[i] > second && nums[i] < max) {
                    third = second;
                    second = nums[i];
                }
                if (nums[i] > max) {
                    third = second;
                    second = max;
                    max = nums[i];
                }
            }
        }
        return third;
    }

    public static void main(String[] args) {
        ThirdBig solution = new ThirdBig();
        int[] nums = { 1, 2, 2, 5, 3, 5 };
        System.out.println(solution.thirdMax(nums));
    }

}